package com.kfm.service.impl;

import com.kfm.domain.User;
import com.kfm.ex.ServiceException;
import com.kfm.mapper.UserMapper;
import com.kfm.service.UserService;
import com.kfm.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper mapper;

    @Override
    public User login(User user) {
        if (ObjectUtils.isEmpty(user) || !StringUtils.hasLength(user.getUsername()) || !StringUtils.hasLength(user.getPassword())){
            throw new ServiceException("用户名或密码不能为空");
        }

        // 密码加密
        user.setPassword(MD5Util.encode(user.getPassword()));

        List<User> users = mapper.selectByUser(user);

        return users.size() > 0 ? users.get(0) : null;
    }
}
